package com.ooqiu.gaming.ui.main;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.ooqiu.gaming.R;
import com.ooqiu.gaming.api.GamingApi;
import com.ooqiu.gaming.base.BaseSupportActivity;
import com.ooqiu.gaming.ui.dialog.IndexTaskDialog;
import com.ooqiu.gaming.ui.main.thread.index.IndexFragment;
import com.ooqiu.gaming.ui.main.thread.interact.InteractFragment;
import com.ooqiu.gaming.ui.main.thread.person.PersonFragment;
import com.ooqiu.gaming.ui.main.thread.video.VideoFragment;
import com.ooqiu.gaming.utils.SettingPrefUtil;
import com.umeng.socialize.UMShareAPI;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页
 *
 * @author Eenie
 */
@Route(path = MainActivity.PATH)
public class MainActivity extends BaseSupportActivity implements RadioGroup.OnCheckedChangeListener {
    public static final String PATH = "/gn/main";
    @BindView(R.id.container)
    FrameLayout mContainer;
    @BindView(R.id.rb_index)
    RadioButton mRbIndex;
    @BindView(R.id.rb_video)
    RadioButton mRbVideo;
    @BindView(R.id.rb_interact)
    RadioButton mRbInteract;
    @BindView(R.id.rb_person)
    RadioButton mRbPerson;
    @BindView(R.id.rg_bottom_bar)
    RadioGroup mRgBottomBar;
    private boolean mIsExit;
    GamingApi mGamingApi;
    String mSearchKeyWord;

    @Override
    protected int toolbarIdRes() {
        return 0;
    }

    @Override
    protected int statusLayoutId() {
        return 0;
    }

    @Override
    public int initContentView() {
        return R.layout.activity_main;
    }

    @Override
    public void initInjector() {
        ButterKnife.bind(this);
        mGamingApi = getApplicationComponent().getGamingApi();
    }

    @Override
    public void initUiAndListener() {
        mRgBottomBar.setOnCheckedChangeListener(this);
        showFragment(R.id.rb_index);
        long lastTime = SettingPrefUtil.getIndexAlertTime(mContext) + 7 * 24 * 60 * 60 * 1000;
        long curTime = System.currentTimeMillis();


//        LogUtil.e(String.format("last = %s cur = %s", lastTime, curTime));
        if (curTime - lastTime > 0) {
            showAlert();
        }

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return false;
    }

//    @Override
//    public void onBackPressed() {
//        if (mIsExit) {
//            this.finish();
//        } else {
//            Toast.makeText(this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
//            mIsExit = true;
//            new Handler().postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    mIsExit = false;
//                }
//            }, 2000);
//        }
//    }


    @Override
    public void onBackPressedSupport() {

        if (mIsExit) {
            super.onBackPressedSupport();
        } else {
            Toast.makeText(this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
            mIsExit = true;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mIsExit = false;
                }
            }, 2000);
        }

    }

    private void showFragment(int checkId) {
        hideFragment(getSupportFragmentManager());
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        Fragment targetFragment = null;
        switch (checkId) {
            case R.id.rb_index:
                targetFragment = getSupportFragmentManager().findFragmentByTag(IndexFragment.PATH);
                if (targetFragment == null) {
                    targetFragment = (Fragment) getRouter().build(IndexFragment.PATH).navigation();
                    ft.add(mContainer.getId(), targetFragment, IndexFragment.PATH);
                }
                break;
            case R.id.rb_video:
                targetFragment = getSupportFragmentManager().findFragmentByTag(VideoFragment.PATH);
                if (targetFragment == null) {
                    targetFragment = (Fragment) getRouter().build(VideoFragment.PATH).navigation();
                    ft.add(mContainer.getId(), targetFragment, VideoFragment.PATH);
                }
                break;
            case R.id.rb_interact:
                targetFragment = getSupportFragmentManager().findFragmentByTag(InteractFragment.PATH);
                if (targetFragment == null) {
                    targetFragment = (Fragment) getRouter().build(InteractFragment.PATH).navigation();
                    ft.add(mContainer.getId(), targetFragment, InteractFragment.PATH);
                }
                break;
            case R.id.rb_person:
                targetFragment = getSupportFragmentManager().findFragmentByTag(PersonFragment.PATH);
                if (targetFragment == null) {
                    targetFragment = (Fragment) getRouter().build(PersonFragment.PATH).navigation();
                    ft.add(mContainer.getId(), targetFragment, PersonFragment.PATH);
                }
                break;
        }

        ft.show(targetFragment);
        ft.commit();
    }

    private void hideFragment(FragmentManager manager) {
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        for (Fragment fragment : manager.getFragments()) {
            fragmentTransaction.hide(fragment);
        }
        fragmentTransaction.commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
        showFragment(checkId);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }


    private void showAlert() {
        IndexTaskDialog dialog = new IndexTaskDialog();
        dialog.setOnEnterClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRbPerson.setChecked(true);
//                showFragment(R.id.rb_person);
            }
        });

        dialog.show(getSupportFragmentManager());
    }


//    private void fetchSearchWordFromNet() {
//        mGamingApi.searchKeyWord(1)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new ProgressSubscriber<List<String>>(mContext) {
//                    @Override
//                    public void onStart() {
//
//                    }
//
//                    @Override
//                    public void onNext(List<String> strings) {
//                        if (!strings.isEmpty()) {
//                            mSearchKeyWord = strings.get(0);
//                            Bundle bundle = new Bundle();
//                            bundle.putString("keyword", mSearchKeyWord);
//                            LocalBroadcastManagerUtil.post(mContext, Constants.ACTION_KEYWORD_CHANGE, bundle);
//                        }
//                    }
//
//                    @Override
//                    public void onFinish() {
//
//                    }
//                });
//    }


//    public void refreshKeyWord() {
//        if (TextUtils.isEmpty(mSearchKeyWord)) {
//            fetchSearchWordFromNet();
//        } else {
//            Bundle bundle = new Bundle();
//            bundle.putString("keyword", mSearchKeyWord);
//            LocalBroadcastManagerUtil.post(mContext, Constants.ACTION_KEYWORD_CHANGE, bundle);
//        }
//    }

}
